package com.ternnetwork.core.configuration.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.Predicate;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.core.configuration.dto.IdGeneratorQueryDto;
import com.ternnetwork.core.configuration.entity.IdGenerator;
import com.ternnetwork.core.configuration.enums.IdGeneratorMessages;
import com.ternnetwork.core.configuration.repository.IdGeneratorRepository;
import com.ternnetwork.core.configuration.service.IdGeneratorService;

@Service
public class IdGeneratorServiceImpl implements IdGeneratorService {
	
	@Autowired
	private IdGeneratorRepository idGeneratorRepository;
	
	@Override
	public BaseResponse getOne(Long id) {
		
		BaseResponse  baseResponse=new BaseResponse();
		
		IdGenerator t=idGeneratorRepository.getOne(id);
		
		baseResponse.setSccucess(t);
		
		return baseResponse;
	}
	@Override
    public BaseResponse add(IdGenerator t) {
    	
    	BaseResponse  baseResponse=checkProperties(t);
    	
    	if(baseResponse.getStatus().intValue()==0) {
    		return baseResponse;
    	}
    	
    	long totalCount=idGeneratorRepository.getCountByName(t.getGenName()); 
    	
      	if(totalCount>0L) {
    		baseResponse.setFailed(IdGeneratorMessages.FAILED_MESSAGE_NAME_CAN_NOT_BE_DUPLICATE.getName());
    		return baseResponse;
    	}
      	
    	t.setGenName(t.getGenName().toUpperCase());
    	
    	idGeneratorRepository.saveAndFlush(t);
		
		baseResponse.setSccucess(t);
		
		return baseResponse;
	}
    
	@Override
    public BaseResponse update(IdGenerator t) {
   	 
        BaseResponse  baseResponse=checkProperties(t);
    	//校验不通过
    	if(baseResponse.getStatus().intValue()==0) {
    		return baseResponse;
    	}
    	
    	long totalCount=idGeneratorRepository.getCountByNameAndExcludeId(t.getGenName(), t.getId()); 
    	
      	if(totalCount>0L) {
    		baseResponse.setFailed(IdGeneratorMessages.FAILED_MESSAGE_NAME_CAN_NOT_BE_DUPLICATE.getName());
    		return baseResponse;
    	}
    	
    	IdGenerator old=idGeneratorRepository.getOne(t.getId());
    	
    	if(old==null||old.getId().longValue()==0L) {
    		baseResponse.setFailedForObjectNotExist();
    		return baseResponse;
    	}
    	
    	old.setGenName(t.getGenName().toUpperCase());
    	old.setGenValue(t.getGenValue());
    	old.setMemo(t.getMemo());
    	
    	idGeneratorRepository.saveAndFlush(old);
		baseResponse.setSccucess(t);
		return baseResponse;
	}
	@Override
    public BaseResponse findByPage(Pageable pageable,IdGeneratorQueryDto dto) {
    	  
    	 BaseResponse  baseResponse=new BaseResponse();
    	
     	 Page<IdGenerator> page = idGeneratorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {

         List<Predicate> predicates = new ArrayList<>();

         if(!StringUtils.isEmpty(dto.getGenName())){
             predicates.add(criteriaBuilder.like(root.get("genName").as(String.class),"%".concat(dto.getGenName().toUpperCase()).concat("%")));
         }

         Predicate[] predicateArray = new Predicate[predicates.size()];

         return criteriaBuilder.and(predicates.toArray(predicateArray));
     }, pageable);
     	 
     	baseResponse.setSccucess(page);
    	
    	return baseResponse;
    }
    
    
	@Override
    public BaseResponse findAll(IdGeneratorQueryDto dto) {
  	  
   	    BaseResponse  baseResponse=new BaseResponse();
   	
    	List<IdGenerator> itemList = idGeneratorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {

        List<Predicate> predicates = new ArrayList<>();

        if(!StringUtils.isEmpty(dto.getGenName())){
            predicates.add(criteriaBuilder.like(root.get("genName").as(String.class),"%".concat(dto.getGenName()).concat("%")));
        }

        Predicate[] predicateArray = new Predicate[predicates.size()];

        return criteriaBuilder.and(predicates.toArray(predicateArray));
    });
    	 
    	baseResponse.setSccucess(itemList);
   	
   	    return baseResponse;
   }
	
	@Override
    public BaseResponse deleteById(Long id) {
		
		BaseResponse  baseResponse=new BaseResponse();
		
		IdGenerator old=idGeneratorRepository.getOne(id);
		
		if(old==null||old.getId().longValue()==0L) {
    		baseResponse.setFailedForObjectNotExist();
    		return baseResponse;
    	}
		if(old.getId().longValue()==1L) {
    		baseResponse.setFailed(IdGeneratorMessages.FAILED_MESSAGE_OBJECT_CAN_NOT_BE_DELETED.getName());
    		return baseResponse;
    	}
		
		idGeneratorRepository.delete(old);
		
		baseResponse.setSccucess();
		
		return baseResponse;
	}
	
     private BaseResponse checkProperties (IdGenerator t) {
    	 
		    BaseResponse  baseResponse=new BaseResponse();
		    
		    if(StringUtils.isEmpty(t.getGenName())) {
		    	baseResponse.setFailed(IdGeneratorMessages.FAILED_MESSAGE_NAME_CAN_NOT_BE_EMPTY.getName());
		    	return baseResponse;
		    }
		
		    if(t.getGenValue()<=0L) {
		    	baseResponse.setFailed(IdGeneratorMessages.FAILED_MESSAGE_VALUE_MUST_BE_MORE_THAN_0.getName());
		    	return baseResponse;
		    }
		    baseResponse.setSccucess();
		    return baseResponse;
      }

}
